package shell.view.components;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
/**@author Rafael Rocha*/
public class DrawPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private List<Drawable> drawables;
	private List<Drawable> addedDrawables;
	private List<Drawable> removedDrawables;

	public DrawPanel() {
		drawables = new ArrayList<Drawable>();
		addedDrawables = new ArrayList<Drawable>();
		removedDrawables = new ArrayList<Drawable>();
	}
	
	public void addDrawable(Drawable drawable) {
		this.addedDrawables.add(drawable);
	}
	public void removeDrawable(Drawable drawable) {
		this.removedDrawables.remove(drawable);
	}
	
	public void paintComponent(Graphics g) {
		drawables.addAll(addedDrawables);
		drawables.removeAll(removedDrawables);
		addedDrawables.clear();
		removedDrawables.clear();
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D)g;
		for(Drawable drawable : drawables) {
			drawable.drawOnScreen(g2D);
			if(drawable.isDead()) {
				removedDrawables.add(drawable);
			}
		}
	}
}
